package com.dream.dao.order;

/**
 * 
 * @author Evan
 * @date 2013-8-25
 * 
 */
import org.springframework.stereotype.Repository;

import com.dream.model.order.OnlineOrder;
import com.dream.model.order.OnlineOrder.OrderStatus;
import com.dream.utils.hibernate.DetachedCriteriaBuilder;
import com.dream.utils.hibernate.DreamBaseDAO;
import com.dream.utils.hibernate.PagingVO;
import com.dream.vo.order.OrderQueryVO;

@Repository
public class OnlineOrderDAO extends DreamBaseDAO<OnlineOrder> {
	
		
		public PagingVO query(final OrderQueryVO vo, final PagingVO page){
			
			DetachedCriteriaBuilder queryBuilder = initQueryBuilder(vo);
			DetachedCriteriaBuilder countBuilder = initQueryBuilder(vo);
			return this.selectPagingVO(queryBuilder, page, countBuilder);
			
		}
		
		private DetachedCriteriaBuilder initQueryBuilder(final OrderQueryVO vo){
			
			DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(OnlineOrder.class);
			builder.innerJoin("createUserObj", "cu");
			builder.innerJoin("orderProduct", "op").innerJoin("op.customer", "oc");
			builder.addIn("oc.orgId", vo.getOrgs()).addLikeAny("oc.name", vo.getCustomerName());
			builder.addEq("status", OrderStatus.getInstance(vo.getStatus())).addLikeAny("cu.name", vo.getSubmitUser());
			builder.addEq("type", vo.getType()).addLikeAny("code", vo.getCode()).addEq("orderMobile", vo.getMobile());
			builder.addGe("orderDate", vo.getStart()).addLe("orderDate", vo.getEnd());
			builder.addIn("handleShop", vo.getShops());
			return builder;
			
		}
		
		public OnlineOrder getById(Long id){
			
			DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(OnlineOrder.class);
			builder.addEq("deleted", false);
			builder.innerJoin("orderProduct", "op").innerJoin("op.customer", "oc").innerJoin("op.product", "p");
			return this.selectTopOne(builder);
			
		}
		
}
